﻿''' <summary>
''' 修改控制器的网络地址
''' </summary>
''' <remarks></remarks>
Public Class NetAddressConfigForm

    ''' <summary>
    ''' nudMAC失去焦点时刷新该设备的当前网络地址
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub nudPAC_LostFocus(ByVal sender As Object, ByVal e As System.EventArgs) Handles nudPAC.LostFocus
        'ReadNetAddress()
    End Sub

    ''' <summary>
    ''' 读取指定PAC的设备的网络地址
    ''' </summary>
    ''' <remarks></remarks>
    Private Sub ReadNetAddress()
        Dim mac As Integer = Me.nudPAC.Value

        Dim _netID As Byte = 0
        Dim funcCode As Byte = FunctionCode.WordWritableRead
        Dim offset As UShort = mac
        Dim count As Byte = 1

        Me.Cursor = Cursors.WaitCursor

        Dim buf() As Short
        buf = COMHelper.Instance.Send4Short(_netID, funcCode, offset, count)

        Me.Cursor = Cursors.Default

        If buf Is Nothing OrElse buf.Length <> 1 Then
            Me.lblOldNetAddress.Text = "无法读取数据"
            Exit Sub
        End If

        Me.lblOldNetAddress.Text = buf(0).ToString()

    End Sub


    Private Sub btnClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnClose.Click
        Me.Close()
    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
        '检查输入MAC地址或网络地址的有效性
        If checkValidate() = False Then
            Exit Sub
        End If

        Me.Cursor = Cursors.WaitCursor

        Dim result As Boolean


        Me.btnRefresh.Enabled = False
        'Me.btnClose.Enabled = False
        Me.btnSave.Enabled = False
        Me.nudPAC.Enabled = False
        Me.nudNetAddress.Enabled = False
        result = Save()

        If result = True Then
            ReadNetAddress()
            msg.Show("修改成功")
        Else
            msg.ShowError("修改失败")
        End If

        Me.Cursor = Cursors.Default

        Me.nudPAC.Enabled = True
        Me.nudNetAddress.Enabled = True
        Me.btnRefresh.Enabled = True
        'Me.btnClose.Enabled = True
        Me.btnSave.Enabled = True

    End Sub

    ''' <summary>
    ''' 检查输入数据的有效性
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function CheckValidate() As Boolean
        If Me.lblOldNetAddress.Text.Trim = "无法读取数据" Then
            msg.ShowWarning(String.Format("无法连接到PAC地址为 {0:X} 的设备，请检查PAC地址是否正确或者设备是否正确连接。", CInt(Me.nudPAC.Value)))
            Me.nudPAC.Focus()
            Return False
        End If

        If Me.nudNetAddress.Value < 1 OrElse Me.nudNetAddress.Value > 255 Then
            msg.ShowWarning("输入的网络地址无效")
            Me.nudNetAddress.Focus()
            Return False
        End If

        Return True
    End Function

    ''' <summary>
    ''' 修改网络地址
    ''' </summary>
    ''' <returns></returns>
    ''' <remarks></remarks>
    Private Function Save() As Boolean
        Dim _netID As Byte = 0  '网络地址（广播地址）
        Dim funcCode As Byte = FunctionCode.WordWrite    '写功能码
        Dim offset As UShort = Me.nudPAC.Value          'MAC地址
        Dim value As Byte = Me.nudNetAddress.Value      '网络地址

        Return COMHelper.Instance.Write(_netID, funcCode, offset, value)
    End Function

    ''' <summary>
    ''' 刷新指定PAC地址的设备的网络地址
    ''' </summary>
    ''' <param name="sender"></param>
    ''' <param name="e"></param>
    ''' <remarks></remarks>
    Private Sub btnRefresh_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRefresh.Click
        Me.lblOldNetAddress.Text = ""
        Me.btnRefresh.Enabled = False
        'Me.btnClose.Enabled = False
        Me.btnSave.Enabled = False
        Me.nudPAC.Enabled = False
        Me.nudNetAddress.Enabled = False

        ReadNetAddress()

        Me.nudPAC.Enabled = True
        Me.nudNetAddress.Enabled = True
        Me.btnRefresh.Enabled = True
        'Me.btnClose.Enabled = True
        Me.btnSave.Enabled = True

    End Sub

End Class